﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Text.RegularExpressions;

using libPlugin;

namespace standardCommands
{
    public class stdPlugin : IBotPlugin
    {

        public string Name
        {
            get { return "Standard Commands"; }
        }

        public string Description
        {
            get { return "Provides a basic level of functionality for tinterBot R2"; }
        }

        public List<BotCommand> Commands { get; set; }

        public List<BotJob> waitingJobs { get; set; }

        public List<BotJob> completedJobs { get; set; }

        public void workerMethod()
        {
            while (true)
            {
                if (waitingJobs.Count == 0)
                {
                    try
                    {
                        Thread.Sleep(Timeout.Infinite);
                    }
                    catch (ThreadInterruptedException)
                    {
                    }
                }
                else
                {
                    List<int> completedJobInts = new List<int>();
                    for (int i = 0; i < waitingJobs.Count; i++)
                    {
                        BotJob job = waitingJobs[i];
                        job.jobResult = waitingJobs[i].command.controller(waitingJobs[i].commandToProcess);

                        waitingJobs.RemoveAt(i);
                        completedJobs.Add(job);
                    }

                    foreach (int num in completedJobInts)
                    {
                        waitingJobs.RemoveAt(num);
                    }
                }
            }
        }

        public stdPlugin()
        {
            Commands = new List<BotCommand>();
            waitingJobs = new List<BotJob>();
            completedJobs = new List<BotJob>();

            Commands.Add(new cmdVersion());
        }
    }

    public class cmdVersion : BotCommand
    {
        public override string helpInformation
        {
            get { return "Returns the Version of tinterBot R2 running."; }
        }

        public override BotResult controller(string command)
        {
            string room = Regex.Match(command, "(?<=PRIVMSG )\\#[A-Za-z0-9]*").ToString();
            Version ver = Assembly.GetEntryAssembly().GetName().Version;

            BotResult res = new BotResult();
            res.ConWrite = "";
            res.LogWrite = "";
            res.NetWrite = string.Format("PRIVMSG {0} :TinterBot R2 v{1}.{2} (b{3} r{4})\r\n", room, ver.Major, ver.Minor, ver.Build, ver.Revision);

            return res;
        }

        public cmdVersion() : base()
        {
            commandSyntax = new Regex("\\?version");
        }
    }
}
